import request from '@/utils/request'

// 查询打卡记录列表
export function listAttendance(query) {
  return request({
    url: '/system/attendance/list',
    method: 'get',
    params: query
  })
}

// 查询打卡记录详细
export function getAttendance(attendanceId) {
  return request({
    url: '/system/attendance/' + attendanceId,
    method: 'get'
  })
}

// 新增打卡记录
export function addAttendance(data) {
  return request({
    url: '/system/attendance',
    method: 'post',
    data: data
  })
}

// 修改打卡记录
export function updateAttendance(data) {
  return request({
    url: '/system/attendance',
    method: 'put',
    data: data
  })
}

// 删除打卡记录
export function delAttendance(attendanceId) {
  return request({
    url: '/system/attendance/' + attendanceId,
    method: 'delete'
  })
}

// 用户打卡签到
export function clockIn(data) {
  // 根据后端Controller的参数要求构建请求数据
  const requestData = {
    clockInLocation: data.clockInLocation || '公司',
    remark: data.remark || ''
  };
  
  console.log('签到请求参数:', requestData);
  
  return request({
    url: '/system/attendance/clockIn',
    method: 'post',
    data: requestData
  })
}

// 用户打卡签退
export function clockOut(data) {
  // 检查必要参数
  if (!data || (!data.attendanceId && !data.id)) {
    console.error('签退失败: 缺少考勤记录ID');
    return Promise.reject('缺少考勤记录ID');
  }
  
  // 确保ID是数字类型
  let recordId;
  try {
    // 优先使用id，如果没有则使用attendanceId
    const rawId = data.id || data.attendanceId;
    recordId = typeof rawId === 'string' ? 
      parseInt(rawId, 10) : rawId;
      
    if (isNaN(recordId)) {
      throw new Error('Invalid ID');
    }
  } catch (error) {
    console.error('签退失败: 考勤记录ID格式错误');
    return Promise.reject('考勤记录ID格式错误');
  }
  
  // 构建请求数据
  const requestData = {
    attendanceId: recordId,  // 后端可能仍然需要attendanceId
    clockOutLocation: data.location || data.clockOutLocation || '公司',  // 支持两种参数名
    remark: data.remark || ''
  };
  
  console.log('签退请求参数:', JSON.stringify(requestData));
  
  return request({
    url: '/system/attendance/clockOut',
    method: 'post',
    data: requestData
  }).catch(error => {
    console.error('签退请求发生错误:', error);
    return Promise.reject(error);
  });
}

// 获取当天打卡状态
export function getTodayStatus(userId) {
  const params = {};
  if (userId) {
    params.userId = userId;
  }
  
  return request({
    url: '/system/attendance/today',
    method: 'get',
    params: params
  })
}

// 获取打卡统计数据
export function getAttendanceStats(params) {
  return request({
    url: '/system/attendance/stats',
    method: 'get',
    params: params
  })
}

// 申请补卡
export function applyReapply(data) {
  return request({
    url: '/system/attendance/reapply',
    method: 'post',
    data: data
  })
}

// 查询补卡申请列表
export function listReapply(query) {
  return request({
    url: '/system/attendance/reapply/list',
    method: 'get',
    params: query
  })
}

// 查询补卡申请详细
export function getReapply(reapplyId) {
  return request({
    url: '/system/attendance/reapply/' + reapplyId,
    method: 'get'
  })
}

// 申请请假
export function applyLeave(data) {
  return request({
    url: '/system/attendance/leave',
    method: 'post',
    data: data
  })
}

// 查询请假申请列表
export function listLeave(query) {
  return request({
    url: '/system/attendance/leave/list',
    method: 'get',
    params: query
  })
}

// 查询请假申请详细
export function getLeave(leaveId) {
  return request({
    url: '/system/attendance/leave/' + leaveId,
    method: 'get'
  })
} 